<?php

namespace Largezhou\Admin\Form\Fields;

use Illuminate\Contracts\Support\Arrayable;
use Largezhou\Admin\Form\Field;

class Select extends Field
{
    protected $props = [
        'clearable'   => false,
        'options'     => [],
        'placeholder' => '',
    ];

    public function options($options)
    {
        if ($options instanceof Arrayable) {
            $options = $options->toArray();
        }

        $options = (array) $options;

        // 把options格式化成前端所需的结构
        $options = array_map(function ($value, $label) {
            return compact('value', 'label');
        }, array_keys($options), $options);

        $this->props('options', $options);

        return $this;
    }
}
